/*
 * @# UploadController.java 2011-12-17 上午10:12:05
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.px.common.action;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.ianswer.px.utils.UserFileUtils;

/**
 * @autesthor lunx
 */
@Controller
public class UploadController {

	private static final Logger log = Logger.getLogger(UploadController.class);

	@RequestMapping(value = "/images/upload", method = RequestMethod.GET)
	public String upload() {
		return "upload";
	}

	@RequestMapping(value = "/images/upload", method = RequestMethod.POST)
	public @ResponseBody
	String upload(@RequestParam("file") MultipartFile file) {
		StringBuilder sb = new StringBuilder();
		if (file.isEmpty()) {
			return "none upload files...";
		}
		try {
			String newFileName = UserFileUtils.genFileName(file.getOriginalFilename());
			String dir = "F:/images/";
			sb.append("{name:'" + file.getOriginalFilename() + "',");
			sb.append("size:'" + file.getSize() + "',");
			sb.append("path:'" + dir + newFileName + "',");
			FileUtils.copyInputStreamToFile(file.getInputStream(), new File(dir + newFileName));
			sb.append("success:1}");
		} catch (IOException e) {
			sb.append("success:0}");
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		return "upload failed...";
	}

	@RequestMapping(value = "/images/uploadMulti", method = RequestMethod.POST)
	public @ResponseBody
	String uploadMulti(MultipartHttpServletRequest request) {
		List<MultipartFile> files = request.getFiles("file");
		StringBuilder sb = new StringBuilder();
		if (CollectionUtils.isEmpty(files)) {
			return "none upload files...";
		}
		for (int i = 0; i < files.size(); i++) {
			if (!files.get(i).isEmpty()) {
				try {
					String newFileName = UserFileUtils.genFileName(files.get(i).getOriginalFilename());
					String dir = "F:/images/";
					sb.append("{name:'" + files.get(i).getOriginalFilename() + "',");
					sb.append("size:'" + files.get(i).getSize() + "',");
					sb.append("path:'" + dir + newFileName + "',");
					FileUtils.copyInputStreamToFile(files.get(i).getInputStream(), new File(dir + newFileName));
					sb.append("success:1}");
				} catch (IOException e) {
					sb.append("success:0}");
					e.printStackTrace();
					log.error(e.getMessage(), e);
				}
			}
		}
		return sb.toString();
	}
}
